package com.example.kotlindemo

import android.content.res.Configuration.UI_MODE_NIGHT_YES
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.animation.core.Spring
import androidx.compose.animation.core.animateDpAsState
import androidx.compose.animation.core.spring
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.icons.Icons.Filled
import androidx.compose.material.icons.filled.ExpandLess
import androidx.compose.material.icons.filled.ExpandMore
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.kotlindemo.ui.theme1.AppTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        enableEdgeToEdge()
        setContent {
            AppTheme {
                Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
                    LazyColumn(
                        modifier = Modifier.padding(innerPadding), // 为 LazyColumn 设置内边距
                        verticalArrangement = Arrangement.spacedBy(16.dp) // 设置垂直间距
                    ) {
                        items(20) { i -> // 使用 items 来处理每一项
                            Greeting(
                                name = "第 $i 个元素",
                                modifier = Modifier.fillMaxWidth() // 确保每个元素占满宽度
                            )
                        }
                    }
                }
            }
        }
    }
}

@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {


    val expanded = rememberSaveable { mutableStateOf(false) }

    val extraPadding by animateDpAsState(
        if (expanded.value) 128.dp else 0.dp,
        animationSpec = spring(
            dampingRatio = Spring.DampingRatioMediumBouncy,
            stiffness = Spring.StiffnessLow
        )
    )

    Surface(
        color =MaterialTheme.colorScheme.primary,
    ) {
        Row(modifier.fillMaxWidth().padding(10.dp)) {
            Column(
                modifier = modifier.padding(0.dp,0.dp)
//                    .background(Color.Blue)
                    .weight(1F),
                    verticalArrangement = Arrangement.spacedBy(0.dp)
            ) {
                Text(
                    text = "aRow1 $name!",
                    modifier = modifier.padding(0.dp).height(44.dp),
                    style = MaterialTheme.typography.labelLarge.copy(
                        fontWeight = FontWeight.Bold
                    )
                )
                Text(
                    text = "aRow2 $name!",
                    modifier = modifier.background(MaterialTheme.colorScheme.tertiary).padding(0.dp).height(extraPadding)
//                    modifier = modifier
                )
            }
            IconButton(
                onClick = { /* TODO */
                    expanded.value = !expanded.value
                    Log.i("my", "click"+name)
                }
            ) {
                Icon(
                    imageVector= if (expanded.value) Filled.ExpandLess else Filled.ExpandMore,
                    contentDescription=if (expanded.value) {
                        stringResource(R.string.show_less)
                    } else {
                        stringResource(R.string.show_more)
                    }
                );
//                Text(if (expanded.value) "收起" else "展开")
            }
        }
    }
}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    AppTheme {
        Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
            LazyColumn(
                modifier = Modifier.padding(innerPadding), // 为 LazyColumn 设置内边距
                verticalArrangement = Arrangement.spacedBy(16.dp) // 设置垂直间距
            ) {
                items(20) { i -> // 使用 items 来处理每一项
                    Greeting(
                        name = "第 $i 个元素",
                        modifier = Modifier.fillMaxWidth() // 确保每个元素占满宽度
                    )
                }
            }
        }
    }
}

@Preview(
    showBackground = true,
    uiMode = UI_MODE_NIGHT_YES,
    )
@Composable
fun GreetingPreviewDark() {
    AppTheme {
        Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
            LazyColumn(
                modifier = Modifier.padding(innerPadding), // 为 LazyColumn 设置内边距
                verticalArrangement = Arrangement.spacedBy(16.dp) // 设置垂直间距
            ) {
                items(20) { i -> // 使用 items 来处理每一项
                    Greeting(
                        name = "第 $i 个元素",
                        modifier = Modifier.fillMaxWidth() // 确保每个元素占满宽度
                    )
                }
            }
        }
    }
}